/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.model.shop;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import l1j.server.Config;
import l1j.server.server.model.L1TaxCalculator;
import l1j.server.server.templates.L1ShopItem;

class L1ShopBuyOrder {
	private final L1ShopItem _item;
	private final int _count;
	private final int _deleteDay; // 道具天數刪除系統(指定天數)
	private final int _remainingTime; //opqlo 使用時間限制

	private final Timestamp _deleteDate; // 道具天數刪除系統(指定日期)
	
	public L1ShopBuyOrder(final L1ShopItem item,final int count,final int deleteDay,final Timestamp deleteDate, final int RemainingTime) { // 道具天數刪除系統
		//public L1ShopBuyOrder(L1ShopItem item, int count) {
		_item = item;
		_count = count;
		_deleteDay = deleteDay; // 道具天數刪除系統(指定天數)
		_deleteDate = deleteDate; // 道具天數刪除系統(指定日期)
		_remainingTime =RemainingTime;//opqlo 使用時間限制
	}

	public L1ShopItem getItem() {
		return _item;
	}

	public int getCount() {
		return _count;
	}
	// 道具天數刪除系統(指定天數)
	public int getDeleteDay() {
		return _deleteDay;
	}

	// 道具天數刪除系統(指定日期)
	public Timestamp getDeleteDate() {
		return _deleteDate;
	}
	
	/**opqlo 使用時間限制*/
	public int getRemainingTime(){
		return _remainingTime;
	}
}

public class L1ShopBuyOrderList {
	private final L1Shop _shop;
	private final List<L1ShopBuyOrder> _list = new ArrayList<L1ShopBuyOrder>();
	private final L1TaxCalculator _taxCalc;

	private int _totalWeight = 0;
	private int _totalPrice = 0;
	private int _totalPriceTaxIncluded = 0;

	L1ShopBuyOrderList(final L1Shop shop) {
		_shop = shop;
		_taxCalc = new L1TaxCalculator(shop.getNpcId());
	}

	public void add(final int orderNumber, final int count) {
		if (_shop.getSellingItems().size() < orderNumber) {
			return;
		}
		final L1ShopItem shopItem = _shop.getSellingItems().get(orderNumber);

		final int price = (int) (shopItem.getPrice() * Config.RATE_SHOP_SELLING_PRICE);
		// オーバーフローチェック
		if(price == 0){return;}
		//opqlo 防洗
		final int set_count = 2000000000 / shopItem.getPrice();
		if(count > set_count){return;}
		//opqlo 防洗
		final int deleteDay =  shopItem.getDeleteDay(); // 道具天數刪除系統(指定天數)
		final Timestamp deleteDate =  shopItem.getDeleteDate(); // 道具天數刪除系統(指定日期)
		final int RemainingTime = shopItem.getRemainingTime();
		for (int j = 0; j < count; j++) {
			if (price * j < 0) {
				return;
			}
		}
		
		_totalPrice += price * count;
		_totalPriceTaxIncluded += _taxCalc.layTax(price) * count;
		_totalWeight += shopItem.getItem().getWeight() * count
				* shopItem.getPackCount();

		if (shopItem.getItem().isStackable()) {
			//_list.add(new L1ShopBuyOrder(shopItem, count* shopItem.getPackCount()));
			_list.add(new L1ShopBuyOrder(shopItem, count* shopItem.getPackCount(), deleteDay, deleteDate,RemainingTime)); // 道具天數刪除系統
			return;
		}

		for (int i = 0; i < (count * shopItem.getPackCount()); i++) {
			//_list.add(new L1ShopBuyOrder(shopItem, 1));
			_list.add(new L1ShopBuyOrder(shopItem, 1, deleteDay, deleteDate,RemainingTime)); // 道具天數刪除系統
		}
	}

	List<L1ShopBuyOrder> getList() {
		return _list;
	}

	public int getTotalWeight() {
		return _totalWeight;
	}

	public int getTotalPrice() {
		return _totalPrice;
	}

	public int getTotalPriceTaxIncluded() {
		return _totalPriceTaxIncluded;
	}

	L1TaxCalculator getTaxCalculator() {
		return _taxCalc;
	}
}
